/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pacificlms.Email;

import pacificlms.Verifiable.Database.Setting;
import pacificlms.DataHolder;
import pacificlms.Verifiable.Database.*;


import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Calendar;


/**
 *
 * @author Brian Hume
 */
public class EmailManager {
    //fixme clean up email
    public static void HoldReadyEmail(Member Member, Hold Hold)
    {
        Resource Resource = (Resource)Hold.GetRelatedData(Resource.class).get(0);
        
        String Body = "";
        Body += "Dear " + Member.Name(null) + "\n";
        Body += "The resource you placed a hold on, " + Resource.Title(null) + ", is available.";
        Body += "Please pick it up before " + Hold.EndDate(null) + " or it will be placed back in circulation.\n";
        
        CreateEmail(Member.Email(null), "Hold ready for pickup", Body);
    }
    
    public static void CheckoutDueEmail(Member Member, CheckOut CheckOut)
    {
        Copy Copy = (Copy)CheckOut.GetRelatedData(Copy.class).get(0);
        Resource Resource = (Resource)CheckOut.GetRelatedData(Resource.class).get(0);
        
        String Body = "";
        Body += "Dear " + Member.Name(null) + "\n";
        Body += "The " + Resource.Type(null) +" you checked out it due on, " + CheckOut.DueDate(null) + ", is available.";
        
        CreateEmail(Member.Email(null), "Checkout Due", Body);
    }
    
    public static void CheckoutLateEmail(Member Member, CheckOut CheckOut, int daysLate)
    {
        Copy Copy = (Copy)CheckOut.GetRelatedData(Copy.class).get(0);
        Resource Resource = (Resource)CheckOut.GetRelatedData(Resource.class).get(0);
        
        String Body = "";
        Body += "Dear " + Member.Name(null) + "\n";
        Body += "The " + Resource.Type(null) +" you checked out its late by " + daysLate + ".";
        
        CreateEmail(Member.Email(null), "Checkout Late", Body);
    }
    
    public static void ForgotPasswordEmail(Member Member)
    {
        String Body = "Dear " + Member.Name(null) + "\n";
        Calendar Now = Calendar.getInstance();
        
        Body += "Today at " + Now.getTime() +" you requested we email you your password.\n";
        Body += "Your password is \"" + Member.Password(null) +"\".";
        
        CreateEmail(Member.Email(null), "Forgot Library Password", Body);
    }
    
    public static void CreateEmail(String ToEmail, String Subject, String Body)
    {
        try
        {
            
            Setting AccountAddress = DataHolder.GetSetting("Source Email Account");
            //Setting AccountUsername = DataHolder.GetSetting("Source Email User");
            Setting AccountPassword = DataHolder.GetSetting("Source Email Password");
            Setting AccountHost = DataHolder.GetSetting("Source Email Host");
            Setting AccountPort = DataHolder.GetSetting("Source Email Port");

            
            // Get system properties
            Properties properties = System.getProperties();

            // Setup mail server properties
            properties.setProperty("mail.smtp.auth", "true");
            properties.setProperty("mail.smtp.host", AccountHost.Value(null));
            properties.setProperty("mail.smtp.port", AccountPort.Value(null));
            
            final String Username = AccountAddress.Value(null);
            final String Password = AccountPassword.Value(null);
            
            // Get the default Session object.
            Session session = Session.getInstance(properties,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(Username, Password);
                    }
                });

            // Create a default MimeMessage object.
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(AccountAddress.Value(null)));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(ToEmail));
            message.setSubject(Subject);
            message.setText(Body);

            // Send message
            Transport.send(message);
        }
        catch(Exception Ex)
        {
            System.out.println(Ex.toString());
        }
    }
}
